
#include "Public.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// 创建JSON格式数据包  
void create_Json1(DeviceData_status *data, char *jsonBuffer_create);

void create_Json1(DeviceData_status *data, char *jsonBuffer_create) 
{  
    // 使用snprintf将设备数据格式化为JSON字符串  
    snprintf(jsonBuffer_create, MAX_JSON_SIZE,  
//                "{\"device_id\":%d,"
//                "\"device_name\":\"%s\","
//                "\"timestamp\":%ld,"
                "{\"type\":\"%s\","  
                "\"properties\":{\"time1\":%d,"
                "\"time2\":%d,"
                "\"time3\":%d,"
//                "\"time4\":%d,"
                "\"stopcount\":%d,"
                "\"runcount\":%d,"  
                "\"X0\":%d,"
                "\"X1\":%d,"
                "\"X2\":%d,"
                "\"X3\":%d,"
                "\"X4\":%d,"
                "\"X5\":%d,"                  
                "\"Y0\":%d,"
                "\"Y1\":%d,"
                "\"Y2\":%d,"
                "\"Y3\":%d,"                  
                "\"ALARM\":%d}}",                 
//             data->device_id, 
//             data->device_name, 
//             data->timestamp, 
             data->type,  
             data->properties.time1, 
             data->properties.time2, 
             data->properties.time3, 
//             data->properties.time4, 
             data->properties.stopcount, 
             data->properties.runcount,  
             data->properties.X0, 
             data->properties.X1, 
             data->properties.X2, 
             data->properties.X3,
             data->properties.X4, 
             data->properties.X5,             
             data->properties.Y0, 
             data->properties.Y1, 
             data->properties.Y2,
             data->properties.Y3,             
             data->properties.ALARM);  
}  



// 创建JSON格式数据包  
void create_Json2(DeviceData_status *data, char *jsonBuffer_create);

void create_Json2(DeviceData_status *data, char *jsonBuffer_create) 
{  
    // 使用snprintf将设备数据格式化为JSON字符串  
    snprintf(jsonBuffer_create, MAX_JSON_SIZE,  
//                "{\"device_id\":%d,"
//                "\"timestamp\":%ld,"
//                "\"type\":\"%s\"}", 
//                
//                "{\"type\":\"%s,"
//                "\"timestamp\":%ld,"
                "{\"type\":\"%s\"}", 
//             data->device_id, 
//             data->timestamp,  
             data->type);  
}  





void JSON_create_upload (void);//

void JSON_create_upload (void)//
{
    // 初始化设备信息  
       

    // 分别赋值将指针放到预定的数组位置，避免意外  
    device_status.device_name=device_status.name_val;
    device_status.type = device_status.type_val;     
    
    device_status.device_id = SN_numble_H*65536+SN_numble_L;                       // 设备ID  

    if(modle==0)
    {
      device_status.device_name = modle0;                   // 设备名称  
    }
    else if(modle==1)
    {
      device_status.device_name = modle1;                   // 设备名称  
    }
    else if(modle==2)
    {
      device_status.device_name = modle2;                   // 设备名称 
    }  
    else if(modle==3)
    {
      device_status.device_name = modle3;                   // 设备名称 
    }  
    
    else if(modle==10)
    {
      device_status.device_name = modle10;                   // 设备名称 
    } 


    else if(modle==11)
    {
      device_status.device_name = modle11;                   // 设备名称 
    } 

    else if(modle==12)
    {
      device_status.device_name = modle12;                   // 设备名称 
    }     
	
    else if(modle==13)
    {
      device_status.device_name = modle13;                   // 设备名称 
    }    	
	
	
    device_status.timestamp = timestamp_H*65536+timestamp_L;                // 时间戳  
    device_status.type = "upload";                           // 数据类型 
//    device_status.type = "Heart";                           // 数据类型     

    // 初始化参数数据  
//    properties_status properties;           // 声明 
    device_status.properties.time1 = work_time1;                             // 气阀延时0x0301  
    device_status.properties.time2 = work_time2;                             // 第1次冲水0x0302  
    device_status.properties.time3 = work_time3;                             // 气阀打开0x0303 
    device_status.properties.time4 = work_time4;                             // 第2次冲水0x0302     
    device_status.properties.stopcount = stop_times;                            // 循环次数用冲洗次数  
    device_status.properties.runcount = run_times;                             // 运行计数  
    device_status.properties.X0 = in_ch1_level;   //   红外开关 
    device_status.properties.X1 = in_ch2_level;   //   按钮开关0x0202
    device_status.properties.X2 = in_ch3_level;   //   压力信号开关
    device_status.properties.X3 = in_ch4_level;   //   高液位信号
    device_status.properties.X4 = in_ch5_level;   //   中液位信号
    device_status.properties.X5 = in_ch6_level;   //   NC    
    device_status.properties.Y0 = out_ch1_val;    //  水阀 0x0100  
    device_status.properties.Y1 = out_ch2_val;  //   气阀 0x0101
    device_status.properties.Y2 = out_ch3_val;  //   预留0x0102
    device_status.properties.Y3 = out_ch4_val;  //   预留0x0103
    device_status.properties.ALARM = waning_numb;  //   报警代码    

// 将参数数据赋值给设备数据  
//    device_status.properties = properties;      


    // 创建JSON数据包  
    create_Json1(&device_status, jsonBuffer_create);  

}



 




void JSON_create_heart (void)//
{
    // 初始化设备信息  

    // 分别赋值将指针放到预定的数组位置，避免意外  
    device_status.device_name=device_status.name_val;
    device_status.type = device_status.type_val;     
    
    device_status.device_id = SN_numble_H*65536+SN_numble_L;    // 设备ID  

    if(modle==0)
    {
      device_status.device_name = modle0;                   // 设备名称  
    }
    else if(modle==1)
    {
      device_status.device_name = modle1;                   // 设备名称  
    }
    else if(modle==2)
    {
      device_status.device_name = modle2;                   // 设备名称 
    }  
    else if(modle==3)
    {
      device_status.device_name = modle3;                   // 设备名称 
    }  
    
    else if(modle==10)
    {
      device_status.device_name = modle10;                   // 设备名称 
    } 


    else if(modle==11)
    {
      device_status.device_name = modle11;                   // 设备名称 
    } 

    else if(modle==12)
    {
      device_status.device_name = modle12;                   // 设备名称 
    }     
	
    else if(modle==13)
    {
      device_status.device_name = modle13;                   // 设备名称 
    }    	
	

    device_status.timestamp = timestamp_H*65536+timestamp_L;                // 时间戳  
//    device_status.type = "upload";                            // 数据类型 
    device_status.type = "Heart";                               // 数据类型     

    // 创建JSON数据包  
    create_Json2(&device_status, jsonBuffer_create);  

}








 


// 简单的字符串解析函数  
void parseString(char *src, char *dest);
void parseString(char *src, char *dest) 
{  
    char *start = strchr(src, '\"') + 1; // 找到第一个引号  
    char *end = strchr(start, '\"');      // 找到第二个引号  
    size_t length = end - start;  
    strncpy(dest, start, length);  
    dest[length] = '\0'; // 添加字符串结束符  
}  


// 解析整数  

int parseInt(char *src); 
int parseInt(char *src) 
{  
    return atoi(src);  
}  

// 解析长整型  
long parseLong(char *src);
long parseLong(char *src) 
{  
    return atol(src);  
}  



// 解析JSON格式数据包  
int parseJson(char *jsondata, DeviceData_cmd *cmd);  // 0异常1正常
int parseJson(char *jsondata, DeviceData_cmd *cmd)  // 0异常1正常
{  


char *properties_GETStart; 

char *address; 



   // 解析设备ID  
    address=strstr(jsondata, "\"device_id\":");
    if (address) 
    {  
      sscanf(address + 12, "%d", &cmd->device_id);  
    } 
    else {  
      cmd->device_id=0xfffffff1;  //没有查到    
    }
    
//    if(cmd->device_id!=SN_numble_H*65536+SN_numble_L)
//    {
//      return 0; //如果sn不对不继续解析
//    }
//    else //否则继续解析
//    {
//      // 解析设备名称  
//
//   

      
      
      
      address=strstr(jsondata, "\"device_name\":");
      if (address) 
      {  
        parseString(address + 14, cmd->device_name);  
      } else 
      {  
        strcpy(cmd->device_name, "nofind"); // 复制字符串到数组  
      }    
      
      // 解析时间戳      
      address=strstr(jsondata, "\"timestamp\":");
      if (address) 
      {  
        sscanf(address + 12, "%ld", &cmd->timestamp);  
      } 
      else {  
        cmd->timestamp=0xfffffff1;  //没有查到
      }       
    
            
      

      // 解析数据类型      
      address=strstr(jsondata, "\"type\":");
      if (address) 
      {  
        parseString(address + 7, cmd->type); 
      } else 
      {  
        strcpy(cmd->type, "nofind"); // 复制字符串到数组 
        return 0; //没有类型参数就返回
      }     

         


   
  // 解析参数数据起始地址 
      
      address=strstr(jsondata, "\"properties\":");
      if (address) 
      {  
        properties_GETStart = address+ 13;
        cmd->properties_ok=1;
      } 
      else 
      
      {  
        cmd->properties_ok=0;      
      }      
      

  // 解析参数数据     
   
      address=strstr(properties_GETStart, "\"time1\":");
      if (address) 
      {  
        sscanf(address + 8, "%ld", &cmd->properties.time1);  
      } else {  
        cmd->properties.time1=0xfffffff1;  //没有查到
      }    

      address=strstr(properties_GETStart, "\"time2\":");
      if (address) 
      {  
        sscanf(address + 8, "%ld", &cmd->properties.time2);  
      } else {  
        cmd->properties.time2=0xfffffff1;  //没有查到
      }  

      address=strstr(properties_GETStart, "\"time3\":");
      if (address) 
      {  
        sscanf(address + 8, "%ld", &cmd->properties.time3);  
      } else {  
        cmd->properties.time3=0xfffffff1;  //没有查到
      }  
      
      address=strstr(properties_GETStart, "\"time4\":");
      if (address) 
      {  
        sscanf(address + 8, "%ld", &cmd->properties.time4);  
      } else {  
        cmd->properties.time4=0xfffffff1;  //没有查到
      }   
      
      address=strstr(properties_GETStart, "\"stopcount\":");
      if (address) 
      {  
        sscanf(address + 12, "%ld", &cmd->properties.stopcount);  
      } else {  
        cmd->properties.stopcount=0xfffffff1;  //没有查到
      }  
      
      address=strstr(properties_GETStart, "\"runcount\":");
      if (address) 
      {  
        sscanf(address + 11, "%ld", &cmd->properties.runcount);  
      } else {  
        cmd->properties.runcount=0xfffffff1;  //没有查到
      }       
      
      
      
      address=strstr(properties_GETStart, "\"Y0\":");
      if (address) 
      {  
        sscanf(address + 5, "%ld", &cmd->properties.Y0);  
      } else {  
        cmd->properties.Y0=0xfffffff1;  //没有查到
      }  


      
      address=strstr(properties_GETStart, "\"Y1\":");
      if (address) 
      {  
        sscanf(address + 5, "%ld", &cmd->properties.Y1);  
      } else {  
        cmd->properties.Y1=0xfffffff1;  //没有查到
      }   
      
      address=strstr(properties_GETStart, "\"Y2\":");
      if (address) 
      {  
        sscanf(address + 5, "%ld", &cmd->properties.Y2);  
      } else {  
        cmd->properties.Y2=0xfffffff1;  //没有查到
      }  
      
      address=strstr(properties_GETStart, "\"Y3\":");
      if (address) 
      {  
        sscanf(address + 5, "%ld", &cmd->properties.Y3);  
      } else {  
        cmd->properties.Y3=0xfffffff1;  //没有查到
      }  
      
      address=strstr(properties_GETStart, "\"ALARM\":");
      if (address) 
      {  
        sscanf(address + 8, "%ld", &cmd->properties.ALARM);  
      } else {  
        cmd->properties.ALARM=0xfffffff1;  //没有查到
      }        
      
      address=strstr(properties_GETStart, "\"CX\":");
      if (address) 
      {  
        sscanf(address + 5, "%ld", &cmd->properties.CX);  
      } else {  
        cmd->properties.CX=0xfffffff1;  //没有查到
      }        
      
      address=strstr(properties_GETStart, "\"UP\":");
      if (address) 
      {  
        sscanf(address + 5, "%ld", &cmd->properties.UP);  
      } else {  
        cmd->properties.UP=0xfffffff1;  //没有查到
      } 
      
      address=strstr(properties_GETStart, "\"BEEP_ontime\":");
      if (address) 
      {  
        
        sscanf(address + strlen("BEEP_ontime")+3, "%ld", &cmd->properties.BEEP_ontime);  
      } else {  
        cmd->properties.BEEP_ontime=0xfffffff1;  //没有查到
      } 

      address=strstr(properties_GETStart, "\"BEEP_offtime\":");
      if (address) 
      {  
        
        sscanf(address + strlen("BEEP_offtime")+3, "%ld", &cmd->properties.BEEP_offtime);  
      } else {  
        cmd->properties.BEEP_offtime=0xfffffff1;  //没有查到
      } 
      
      
      
      address=strstr(properties_GETStart, "\"BEEP_onoff\":");
      if (address) 
      {  
        
        sscanf(address + strlen("BEEP_onoff")+3, "%ld", &cmd->properties.BEEP_onoff);  
      } else {  
        cmd->properties.BEEP_onoff=0xfffffff1;  //没有查到
      }        
      


      return 1; //解析正确




} 

void HL_limit(u32 *obj,u32 *data,u32 H_limit,u32 L_limit); 
void HL_limit(u32 *obj,u32 *data,u32 H_limit,u32 L_limit) 
{ 
  if(*data!=0xfffffff1)
  {
      if(*data<L_limit && L_limit!=0)
      {
//        *data=L_limit;
        return ; //值错误         
      }
      else if ( *data>H_limit)
      {
//        *data=H_limit;
        return ; //值错误      
      }
      *obj=*data; 
  }

}


u8 JSON_GetObjectItem(void) // 0异常1正常
{  

// 解析JSON数据包 
    if(parseJson(jsonBuffer_get, &device_cmd)!=0) //id正确才解析  0异常1正常
    {
      //处理对应数据更新参数
      //--------------------处理download---------------------- 
      if(STR_CM( device_cmd.type, "download",100)==1)
      {
         u32 H_limit;
          u32 L_limit;

          H_limit=20000;
          L_limit=500;     
          

          HL_limit(&work_time1,&device_cmd.properties.time1,H_limit,L_limit); // 气阀延时0x0301  
          HL_limit(&work_time2,&device_cmd.properties.time2,H_limit,L_limit); // 第1次冲水0x0302 
          HL_limit(&work_time3,&device_cmd.properties.time3,H_limit,L_limit); // 气阀打开0x0303  
          HL_limit(&work_time4,&device_cmd.properties.time4,H_limit,L_limit); // 第2次冲水0x0304       
          
          H_limit=1000;
          L_limit=0;         
          
          HL_limit(&stop_times,&device_cmd.properties.stopcount,H_limit,L_limit); //  
          
          H_limit=4100000000;
          L_limit=0;              
          HL_limit(&run_times,&device_cmd.properties.runcount,H_limit,L_limit); //         
          
          
          H_limit=1;
          L_limit=0;   

          HL_limit(&out_ch1_val,&device_cmd.properties.Y0,H_limit,L_limit); //         
          HL_limit(&out_ch2_val,&device_cmd.properties.Y1,H_limit,L_limit); //         
          HL_limit(&out_ch3_val,&device_cmd.properties.Y2,H_limit,L_limit); //         
          HL_limit(&out_ch4_val,&device_cmd.properties.Y3,H_limit,L_limit); // 
          

          if(device_cmd.properties.ALARM==99)//报警复位
          {
            device_cmd.properties.ALARM=0;
            waning_numb=0;
          }  
          
          if(device_cmd.properties.CX==1)//远程冲洗
          {
            device_cmd.properties.CX=0;
            flush_status=1;
            page=0;
          }            
          
       
          
          
//          H_limit=1000;
//          L_limit=0;           
//          HL_limit(&ALARM_reset_CMD,&device_cmd.properties.ALARM,H_limit,L_limit); // 
//          if(ALARM_reset_CMD==99)
//          {
//            ALARM_reset_CMD=0;
//            waning_numb=0;
//          }
//          
          
          
//          HL_limit(&BEEP_EN,&device_cmd.properties.BEEP_onoff,H_limit,L_limit); // 
          
          if(device_cmd.properties.BEEP_onoff==0)//远程关蜂鸣器
          {
            BEEP_EN=0;
          }
          if(device_cmd.properties.BEEP_onoff==1)//远程开蜂鸣器
          {
            BEEP_EN=1;
          }
          

          
          H_limit=1000;
          L_limit=20;           
          HL_limit(&BEEP_on_time,&device_cmd.properties.BEEP_ontime,H_limit,L_limit); // 
                   
          HL_limit(&BEEP_off_time,&device_cmd.properties.BEEP_offtime,H_limit,L_limit); //           
          
          
          H_limit=1;
          L_limit=0;             
          HL_limit(&UPLOAD_CMD,&device_cmd.properties.UP,H_limit,L_limit); //           
          
//===============回复云平台=====================      
          if(UPLOAD_CMD!=0  )//如果需要应答
          {
            UPLOAD_CMD=0;
            JSON_create_upload();//创建上传数据包
            uart2.Tx1_enable=1;
            uart2.Tx_standard=1;      //要发送的数据是否为标志协议，0 485数据,1json数据       
          }          
          
          gc.WriteData_filish =0;
      } 
      
 
      
//--------------------------查询状态--------------------------      
      else if(STR_CM( device_cmd.type, "GetStatus",100)==1)
      {
        JSON_create_upload();//创建上传数据包
        uart2.Tx1_enable=1;
        uart2.Tx_standard=1;      //要发送的数据是否为标志协议，0 485数据,1json数据       
      }  

//-------------------------开启modbus---------------------- 

      else if(STR_CM( device_cmd.type, "Modbus_on",100)==1)
      {
        MODBUS_internet_EN=1; 
        modbus_exit_cnt=0;
      }  
      
      
//--------------------------关闭modbus---------------------- 

      else if(STR_CM( device_cmd.type, "Modbus_off",100)==1)
      {
        MODBUS_internet_EN=0; 
        modbus_exit_cnt=0;
      }  
            
//--------------------------打开蜂鸣器---------------------- 

      else if(STR_CM( device_cmd.type, "BEEP_on",100)==1)
      {
        BEEP_EN=1; 
      }        
      
//--------------------------关闭蜂鸣器---------------------- 

      else if(STR_CM( device_cmd.type, "BEEP_off",100)==1)
      {
        BEEP_EN=0; 
      }        
      
      
      
      
      
      
      

      return 1; //正常解析
     
    }

    else
    {
      return 0; //0异常      
    }
}  




void JSON (void)//
{
  
//  JSON_create_upload();//创建上传数据包
//  JSON_GetObjectItem();//解析json包 
  

  
  if(Heart_Time_cnt >=Heart_Time)// 4G心跳时间计数器
  {
    Heart_Time_cnt=0;
    JSON_create_heart();//创建上心跳包      
    uart2.Tx1_enable=1;
    uart2.Tx_standard=1;      //要发送的数据是否为标志协议，0 485数据,1json数据        
    
  }

}

